package com.yun.zf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.admin.domain.SysPost;
import com.yun.zf.common.admin.domain.SysUserPost;
import com.yun.zf.common.admin.dto.sys.PostListDto;
import com.yun.zf.common.constant.UserConstants;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.system.exception.CustomException;
import com.yun.zf.system.mapper.SysPostMapper;
import com.yun.zf.system.mapper.SysUserPostMapper;
import com.yun.zf.system.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    @Override
    public List<PostListDto> getPosts() {
       return  baseMapper.getInAgentPost();
    }
    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post)
    {
        QueryWrapper<SysPost> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.lambda().eq(StringUtils.isNotEmpty(post.getPostCode()),SysPost::getPostCode,post.getPostCode())
                .eq(StringUtils.isNotEmpty(post.getPostName()),SysPost::getPostName,post.getPostName())
                .eq(StringUtils.isNotEmpty(post.getStatus()),SysPost::getStatus,post.getStatus());
        return baseMapper.selectList(postQueryWrapper);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll()
    {
        return postMapper.selectPostAll();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Long postId)
    {
        QueryWrapper<SysPost> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.lambda().eq(SysPost::getPostId,postId);
        return baseMapper.selectOne(postQueryWrapper);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    public List<Integer> selectPostListByUserId(Long userId)
    {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostNameUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        QueryWrapper<SysPost> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.lambda().eq(SysPost::getPostName,post.getPostName());
        SysPost info =  baseMapper.selectOne(postQueryWrapper);
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostCodeUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        QueryWrapper<SysPost> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.lambda().eq(SysPost::getPostCode,post.getPostCode());
        SysPost info =  baseMapper.selectOne(postQueryWrapper);
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId)
    {
        return userPostMapper.countUserPostById(postId);
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int deletePostById(Long postId)
    {
        return postMapper.deletePostById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     * @throws Exception 异常
     */
    public int deletePostByIds(Long[] postIds)
    {
        for (Long postId : postIds)
        {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new CustomException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return postMapper.deletePostByIds(postIds);
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPost post)
    {
        post.setCreateTime(new Date());
        return baseMapper.insert(post);
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPost post)
    {
        return postMapper.updatePost(post);
    }

    @Override
    public Long getUserPost(Long userId) {
       SysUserPost userPost = userPostMapper.getUserPosts(userId);
       if (userPost!=null){
           return  userPost.getPostId();
       }
       return  -1L;
    }
}
